package org.jkuang.qstar.index.iterator;

/**
 * 
 * @author jkuang
 * 
 *         implements SubList
 * 
 * @param <T>
 */
public class SubList<T> {
	private final T[] array;
	private final int offset;
	private final int size;
	private final boolean desc;
	private int cursor;

	/**
	 * 
	 * @param array
	 * @param fromIndex
	 * @param toIndex
	 * @param desc
	 */
	public SubList(T[] array, int fromIndex, int toIndex, boolean desc) {
		this.array = array;
		this.size = toIndex - fromIndex + 1;
		this.offset = fromIndex;
		this.desc = desc;
		if (desc) {
			cursor = size - 1;
		}
	}

	public T get(int index) {
		return array[offset + index];
	}

	public int size() {
		return this.size;
	}

	public boolean isEmpty() {
		// TODO Auto-generated method stub
		return this.size <= 0;
	}

	public boolean hasNext() {

		return size > 0 && cursor < size && cursor >= 0;
	}

	public T next() {
		if (desc) {
			return array[offset + cursor--];
		} else {
			return array[offset + cursor++];
		}
	}
}
